package com.spider.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.spider.dao.HBaseDAOImp;
import com.spider.dao.HBaseDao;
import com.spider.model.Option;
import com.spider.model.Question;

public class ResolveHtmlUtils {
	
	private List<String> idList;
	private List<String> diffList;
	private List<String> answUrlList;
	private List<String> contList;
	private List<String> imgUrlList;
	private List<List<String>> contentList;
	private String know;
	private String grade;
	private String type;
	private String version;
	private HBaseDao dao = new HBaseDAOImp();
	private int index = 0;
	private List<String> optionList;
	private List<String> optionContentList;
	private List<List<String>> optionImgUrlList;
	private String optionImgUrlA;
	private String optionImgUrlB;
	private String optionImgUrlC;
	private String optionImgUrlD;

	public void resolve(String html) {
		JsoupUtils jsoup = new JsoupUtils();
		idList = jsoup.resolveId(html);
		contList = jsoup.resolveContentWithImg(html);
		answUrlList = jsoup.resolveAnswerUrl(html);
		diffList = jsoup.resolveDiff(html);
		know = jsoup.resolveKnow(html);
		grade= jsoup.resolveGrad(html);
		type = jsoup.resolveType(html);
		version = jsoup.resolveVersion(html);
		optionList = jsoup.resolveOption(html);

		Question ques = null;
		Option option = null;

		String[] quailifer = new String[] {"id","content","answerUrl","diff","know","grade","type","version","optionA","optionB","optionC","optionD"};
		String[] value = null;
		for(int i = 0;i < contList.size();i ++){
			ques = new Question();
			option = new Option();
			ques.setRowKey(idList.get(i));
			ques.setContent(contList.get(i));
			ques.setAnswerUrl(answUrlList.get(i));
			ques.setDiff(diffList.get(i));
			ques.setKnow(know);
			ques.setGrade(grade);
			ques.setType(type);
			ques.setVersion(version);

			int indexOption = i * 4;

//			System.e6rr.println(ques.getType());
			if("单选题".equals(ques.getType())||"多选题".equals(ques.getType())||"全部".equals(ques.getType())) {
				try {
					if(optionContentList != null) {
						option.setOptionA(optionContentList.get(indexOption));
						option.setOptionB(optionContentList.get(indexOption+1));
						option.setOptionC(optionContentList.get(indexOption+2));
						option.setOptionD(optionContentList.get(indexOption+3));
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			ques.setOption(option);

			value = new String[] {ques.getRowKey(),ques.getContent(),ques.getAnswerUrl(),ques.getDiff(),ques.getKnow()
					,ques.getGrade(),ques.getType(),ques.getVersion()
					,ques.getOption().getOptionA(),ques.getOption().getOptionB()
					,ques.getOption().getOptionC(),ques.getOption().getOptionD()};
			index++;
//			dao.insert("Test1", index+"", "base", quailifer, value);
            System.err.println(ques.getContent());
//            System.out.println(value);
            System.out.println(ques.getOption().getOptionA());
        }
		System.err.println(index);
	}

	
//	public void resolve(String html) {
//		JsoupUtils jsoup = new JsoupUtils();
//		idList = jsoup.resolveId(html);
//		contentList = jsoup.resolveContent(html);
//		contList = contentList.get(0);
//		imgUrlList = contentList.get(1);
//		answUrlList = jsoup.resolveAnswerUrl(html);
//		diffList = jsoup.resolveDiff(html);
//		know = jsoup.resolveKnow(html);
//		grade= jsoup.resolveGrad(html);
//		type = jsoup.resolveType(html);
//		version = jsoup.resolveVersion(html);
//		optionList = jsoup.resolveOption(html);
//		optionContentList = optionList.get(0);
//		optionImgUrlList = optionList.get(1);
//
//		Question ques = null;
//		Option option = null;
//
//		String[] quailifer = new String[] {"content","answerUrl","diff","know","imgUrl","grade","type","version","optionA","optionB","optionC","optionD","optionImgUrlA","optionImgUrlB","optionImgUrlC","optionImgUrlD"};
//		String[] value = null;
//		for(int i = 0;i < contList.size();i ++){
//			ques = new Question();
//			option = new Option();
//			ques.setRowKey(idList.get(i));
//			ques.setContent(contList.get(i));
//			ques.setAnswerUrl(answUrlList.get(i));
//			ques.setDiff(diffList.get(i));
//			ques.setKnow(know);
//			ques.setGrade(grade);
//			ques.setType(type);
//			ques.setVersion(version);
//			if(imgUrlList.get(i)=="") {
//				ques.setImgUrl("");
//			}else {
//				ques.setImgUrl(imgUrlList.get(i));
//			}
//
//			int indexOption = i * 4;
//
////			System.e6rr.println(ques.getType());
//			if("单选题".equals(ques.getType())||"多选题".equals(ques.getType())||"全部".equals(ques.getType())) {
//				try {
//					if(optionContentList != null) {
//						option.setOptionA(optionContentList.get(indexOption));
//						option.setOptionB(optionContentList.get(indexOption+1));
//						option.setOptionC(optionContentList.get(indexOption+2));
//						option.setOptionD(optionContentList.get(indexOption+3));
//
//						StringBuffer bufferA = new StringBuffer();
//						StringBuffer bufferB = new StringBuffer();
//						StringBuffer bufferC = new StringBuffer();
//						StringBuffer bufferD = new StringBuffer();
//
//						for(int j = 0; j <optionImgUrlList.get(indexOption).size();j++) {
//							bufferA.append(optionImgUrlList.get(indexOption).get(j)+"-----");
////							System.out.println(optionImgUrlList.get(indexOption).get(j)+"-----");
////							optionImgUrlA += optionImgUrlList.get(indexOption).get(j)+"-----";
//						}
//
//						for(int j = 0; j <optionImgUrlList.get(indexOption+1).size();j++) {
//							bufferB.append(optionImgUrlList.get(indexOption+1).get(j)+"-----");
////							optionImgUrlB += optionImgUrlList.get(indexOption+1).get(j)+"-----";
//						}
//
//						for(int j = 0; j <optionImgUrlList.get(indexOption+2).size();j++) {
//							bufferC.append(optionImgUrlList.get(indexOption+2).get(j)+"-----");
////							optionImgUrlC += optionImgUrlList.get(indexOption+2).get(j)+"-----";
//						}
//
//						for(int j = 0; j <optionImgUrlList.get(indexOption+3).size();j++) {
//							bufferD.append(optionImgUrlList.get(indexOption+3).get(j)+"-----");
//							optionImgUrlD += optionImgUrlList.get(indexOption+3).get(j)+"-----";
//						}
//
//
//						option.setOptionImgUrlA(bufferA.toString());
//						option.setOptionImgUrlB(bufferB.toString());
//						option.setOptionImgUrlC(bufferC.toString());
//						option.setOptionImgUrlD(bufferD.toString());
//					}
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//
//			ques.setOption(option);
//
//			value = new String[] {ques.getContent(),ques.getAnswerUrl(),ques.getDiff(),ques.getKnow()
//					,ques.getImgUrl(),ques.getGrade(),ques.getType(),ques.getVersion()
//					,ques.getOption().getOptionA(),ques.getOption().getOptionB()
//					,ques.getOption().getOptionC(),ques.getOption().getOptionD()
//					,ques.getOption().getOptionImgUrlA(),ques.getOption().getOptionImgUrlB()
//					,ques.getOption().getOptionImgUrlC(),ques.getOption().getOptionImgUrlD()};
//			index++;
//			dao.insert("Test1", index+"", "base", quailifer, value);
////			System.out.println(ques.getOption().getOptionA());
////			System.out.println(ques.getOption().getOptionB());
////			System.out.println(ques.getOption().getOptionC());
////			System.out.println(ques.getOption().getOptionD());
////			System.out.println(ques.getOption().getOptionImgUrlA());
////			System.out.println(ques.getOption().getOptionImgUrlB());
////			System.out.println(ques.getOption().getOptionImgUrlC());
////			System.out.println(ques.getOption().getOptionImgUrlD());
//		}
//		System.err.println(index);
//	}
	
	public void resolve(File file){
		JsoupUtils jsoup = new JsoupUtils();
		idList = jsoup.resolveId(file);
		contentList = jsoup.resolveContent(file);
		contList = contentList.get(0);
		imgUrlList = contentList.get(1);
		answUrlList = jsoup.resolveAnswerUrl(file);
		diffList = jsoup.resolveDiff(file);
		know = jsoup.resolveKnow(file);
		grade= jsoup.resolveGrad(file);
		type = jsoup.resolveType(file);
		version = jsoup.resolveVersion(file);
//		optionList = jsoup.resolveOption(file);
//		optionContentList = optionList.get(0);
//		optionImgUrlList = optionList.get(1);

		Question ques = null;
		Option option = null;

		String[] quailifer = new String[] {"content","answerUrl","diff","know","imgUrl","grade","type","version","optionA","optionB","optionC","optionD","optionImgUrlA","optionImgUrlB","optionImgUrlC","optionImgUrlD"};
		String[] value = null;
		for(int i = 0;i < contList.size();i ++){
			ques = new Question();
			option = new Option();
			ques.setRowKey(idList.get(i));
			ques.setContent(contList.get(i));
			ques.setAnswerUrl(answUrlList.get(i));
			ques.setDiff(diffList.get(i));
			if(imgUrlList.get(i)=="") {
				ques.setImgUrl("");
			}else {
				ques.setImgUrl(imgUrlList.get(i));
			}

			int indexOption = i * 4;

			if(optionContentList != null) {
				option.setOptionA(optionContentList.get(indexOption));
				option.setOptionB(optionContentList.get(indexOption+1));
				option.setOptionC(optionContentList.get(indexOption+2));
				option.setOptionD(optionContentList.get(indexOption+3));

				for(int j = 0; j <optionImgUrlList.get(indexOption).size();j++) {
					optionImgUrlA += optionImgUrlList.get(indexOption).get(j)+"-----";
				}

				for(int j = 0; j <optionImgUrlList.get(indexOption+1).size();j++) {
					optionImgUrlB += optionImgUrlList.get(indexOption+1).get(j)+"-----";
				}

				for(int j = 0; j <optionImgUrlList.get(indexOption+2).size();j++) {
					optionImgUrlC += optionImgUrlList.get(indexOption+2).get(j)+"-----";
				}

				for(int j = 0; j <optionImgUrlList.get(indexOption+3).size();j++) {
					optionImgUrlD += optionImgUrlList.get(indexOption+3).get(j)+"-----";
				}

				optionImgUrlList.get(indexOption+1);
				optionImgUrlList.get(indexOption+2);
				optionImgUrlList.get(indexOption+3);

				option.setOptionImgUrlA(optionImgUrlA);
				option.setOptionImgUrlB(optionImgUrlB);
				option.setOptionImgUrlC(optionImgUrlC);
				option.setOptionImgUrlD(optionImgUrlD);
			}

			ques.setOption(option);
			ques.setKnow(know);
			ques.setGrade(grade);
			ques.setType(type);
			ques.setVersion(version);
			value = new String[] {ques.getContent(),ques.getAnswerUrl(),ques.getDiff(),ques.getKnow()
					,ques.getImgUrl(),ques.getGrade(),ques.getType(),ques.getVersion()
					,ques.getOption().getOptionA(),ques.getOption().getOptionB()
					,ques.getOption().getOptionC(),ques.getOption().getOptionD()
					,ques.getOption().getOptionImgUrlA(),ques.getOption().getOptionImgUrlB()
					,ques.getOption().getOptionImgUrlC(),ques.getOption().getOptionImgUrlD()};
			index++;
			dao.insert("Test", index+"", "base", quailifer, value);
			System.out.println(ques.getOption().getOptionA());
			System.out.println(ques.getOption().getOptionB());
			System.out.println(ques.getOption().getOptionC());
			System.out.println(ques.getOption().getOptionD());
			System.out.println(ques.getOption().getOptionImgUrlA());
			System.out.println(ques.getOption().getOptionImgUrlB());
			System.out.println(ques.getOption().getOptionImgUrlC());
			System.out.println(ques.getOption().getOptionImgUrlD());
		}
		System.err.println(index);
	}
}

